package com.sxf.djiuav;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

import dji.sdk.keyvalue.value.product.ProductType;
import dji.v5.manager.diagnostic.DJIDeviceStatus;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    private DJIApplication djiApp;
    private TextView statusTextView;
    private TextView productTypeTextView;
    private TextView deviceStatusTextView;
    private TextView firmwareVersionTextView;
    private TextView sdkStatusTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化视图
        initViews();

        // 获取 DJIApplication 实例
        djiApp = (DJIApplication) getApplication();

        // 设置初始化监听器
        setupSDKListener();

        // 检查 SDK 状态（可能已经初始化完成）
        checkSDKStatus();
    }

    /**
     * 初始化视图
     */
    private void initViews() {
        statusTextView = findViewById(R.id.statusTextView);
        productTypeTextView = findViewById(R.id.productTypeTextView);
        deviceStatusTextView = findViewById(R.id.deviceStatusTextView);
        firmwareVersionTextView = findViewById(R.id.firmwareVersionTextView);
        sdkStatusTextView = findViewById(R.id.sdkStatusTextView);

        // 设置初始文本
        statusTextView.setText("正在检查SDK状态...");
        productTypeTextView.setText("产品类型: 未知");
        deviceStatusTextView.setText("设备状态: 未知");
        firmwareVersionTextView.setText("固件版本: 未知");
        sdkStatusTextView.setText("SDK状态: 检查中...");
    }

    /**
     * 设置SDK监听器
     */
    private void setupSDKListener() {
        djiApp.setSDKInitListener(new DJIApplication.SDKInitListener() {
            @Override
            public void onInitSuccess() {
                runOnUiThread(() -> {
                    sdkStatusTextView.setText("SDK状态: 已注册");
                    statusTextView.setText("SDK初始化成功");
                    updateDeviceInfo();
                });
            }

            @Override
            public void onInitFailure(String error) {
                runOnUiThread(() -> {
                    sdkStatusTextView.setText("SDK状态: 注册失败");
                    statusTextView.setText("SDK初始化失败: " + error);
                });
            }

            @Override
            public void onDeviceConnected(ProductType productType) {
                runOnUiThread(() -> {
                    statusTextView.setText("设备已连接");
                    productTypeTextView.setText("产品类型: " + (productType != null ? productType.name() : "未知"));
                    updateDeviceInfo();
                });
            }

            @Override
            public void onDeviceDisconnected() {
                runOnUiThread(() -> {
                    statusTextView.setText("设备已断开连接");
                    productTypeTextView.setText("产品类型: 无");
                    firmwareVersionTextView.setText("固件版本: 无");
                });
            }

            @Override
            public void onDeviceChanged(ProductType oldType, ProductType newType) {
                runOnUiThread(() -> {
                    statusTextView.setText("设备已变更");
                    String oldName = oldType != null ? oldType.name() : "无";
                    String newName = newType != null ? newType.name() : "无";
                    productTypeTextView.setText("产品类型变更: " + oldName + " -> " + newName);
                    updateDeviceInfo();
                });
            }

            @Override
            public void onDeviceStatusUpdated(DJIDeviceStatus from, DJIDeviceStatus to) {
                runOnUiThread(() -> {
                    String fromStatus = from != null ? from.name() : "未知";
                    String toStatus = to != null ? to.name() : "未知";
                    deviceStatusTextView.setText("设备状态: " + fromStatus + " -> " + toStatus);
                });
            }

            @Override
            public void onDatabaseDownloadProgress(long current, long total) {
                runOnUiThread(() -> {
                    int progress = total > 0 ? (int) (current * 100 / total) : 0;
                    statusTextView.setText("数据库下载中: " + progress + "%");
                });
            }
        });
    }

    /**
     * 检查SDK状态
     */
    private void checkSDKStatus() {
        runOnUiThread(() -> {
            if (djiApp.isSDKInitialized()) {
                sdkStatusTextView.setText("SDK状态: 已初始化");
            } else {
                sdkStatusTextView.setText("SDK状态: 未初始化");
            }

            if (djiApp.isSDKRegistered()) {
                sdkStatusTextView.setText(sdkStatusTextView.getText() + ", 已注册");
                statusTextView.setText("SDK已注册");
            } else {
                sdkStatusTextView.setText(sdkStatusTextView.getText() + ", 未注册");
                statusTextView.setText("SDK未注册");
            }

            // 检查设备连接状态
            checkDeviceConnection();
        });
    }

    /**
     * 检查设备连接状态
     */
    private void checkDeviceConnection() {
        runOnUiThread(() -> {
            if (djiApp.isDeviceConnected()) {
                statusTextView.setText("设备已连接");
                updateDeviceInfo();
            } else {
                statusTextView.setText("设备未连接");
            }
        });
    }

    /**
     * 更新设备信息
     */
    private void updateDeviceInfo() {
        runOnUiThread(() -> {
            if (djiApp.isDeviceConnected()) {
                String firmwareVersion = djiApp.getFirmwareVersion();
                String serialNumber = djiApp.getSerialNumber();

                firmwareVersionTextView.setText("固件版本: " + firmwareVersion);
                // 可以显示更多设备信息...
            } else {
                firmwareVersionTextView.setText("固件版本: 无");
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();

        // 检查当前连接状态
        checkDeviceConnection();
        checkSDKStatus();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 清除监听器
        djiApp.setSDKInitListener(null);
    }
}